package com.mianyang.zhangsan.plugin.approval;

import com.mianyang.zhangsan.common.util.JsonUtils;
import com.mianyang.zhangsan.model.dto.SecApprovalDto;
import com.mianyang.zhangsan.model.entity.SecApproval;
import com.mianyang.zhangsan.model.entity.SecRole;
import com.mianyang.zhangsan.service.ISecUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 权限申请审批
 * @author jiangjr
 * @date 2022-10-25
 */
@Component
public class ApplyAuthApproval extends AbstractApprovalHandler{
    @Autowired
    private ISecUserRoleService userRoleService;

    @Override
    public void handle(SecApprovalDto dto) {
        // 获取审批状态
        SecApproval approval = dto.getApproval();
        if (approval.getApprovalStatus() == 1) {
            // 获取角色信息
            SecRole role = null;
            try {
                role = JsonUtils.toBean(dto.getApply(), SecRole.class);
            } catch (Exception e) {
                throw new RuntimeException("角色转换异常");
            }
            // 将角色赋予用户
            if (role != null) {
                userRoleService.grantAuthorities(role.getRoleId(), approval.getSenderId());
            }
        }
    }
}
